const path = require("path");
const {
    CleanWebpackPlugin
} = require('clean-webpack-plugin')

module.exports = (config) => {
    return {
        mode: config.model || 'production',
        devtool: config.devtool || 'source-map',
        entry: `${config.componentRoot}/src/index.js`,
        output: {
            filename: `${config.componentName}.[hash:6].min.js`,
            path: path.resolve(config.componentRoot, 'dist'),
            library: config.componentName,
            libraryTarget: 'umd',
            umdNamedDefine: true
        },
        //for webpack loader path
        resolveLoader: {
            modules: [config.root + "/node_modules"]
        },
        resolve: {
            modules: [
                'node_modules',
                `${config.root}/node_modules`
            ],
            alias: {
                mds: 'morningstar-design-system'
            },
            extensions: ['.js', '.vue', '.json']
        },
        module: {
            rules: [{
                    test: /\.css?$/,
                    use: ['style-loader', 'css-loader']
                },
                {
                    test: /\.scss$/,
                    use: ['style-loader', 'css-loader', 'sass-loader']
                },
                {
                    test: /\.(js|vue)$/,
                    use: 'eslint-loader',
                    enforce: 'pre'
                }, {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader'
                    }
                }, {
                    test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                    use: {
                        loader: 'url-loader',
                        options: {
                            limit: 10 * 1024,
                            esModule: false,
                        }
                    }
                }, {
                    test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
                    use: {
                        loader: 'url-loader',
                        options: {
                            limit: 10000,
                        }
                    }
                }, {
                    test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                    use: {
                        loader: 'url-loader',
                        options: {
                            limit: 10000,
                        }
                    }
                }
            ]
        },

        plugins: [
            new CleanWebpackPlugin(),
        ],
        externals: [
            'mbc-legend',
            'mbc-vertical-bar-css',
            'mbc-vertical-bar',
            'mbc-correlation-matrix-css',
            'mbc-correlation-matrix',
            'mbc-donut-css',
            'mbc-donut',
            'mbc-horizontal-bar-css',
            'mbc-horizontal-bar',
            'mbc-time-series-line-css',
            'mbc-time-series-line',
            'mbc-line-css',
            'mbc-line',
            'mbc-geographical-map-css',
            'mbc-geographical-map',
            'mbc-style-box-css',
            'mbc-style-box',
            'mbc-style-box-equity-css',
            'mbc-style-box-equity',
            'mbc-style-box-fixed-income-css',
            'mbc-style-box-fixed-income',
            'mbc-style-box-market-barometer-css',
            'mbc-style-box-market-barometer',
            'mbc-style-map-css',
            'mbc-style-map',
            'mbc-bubble-css',
            'mbc-bubble',
            'mbc-scatter-plot-css',
            'mbc-scatter-plot',
            'mbg',
            'select2',
            'morningstar-design-system',
            'Backbone',
            'd3',
            'document-register-element',
            'hs-icon',
            'mbc-core-css',
            'mbc-core',
            'mbc-flag-css',
            'mbc-flag',
            'mbc-legend-css',
            {
                underscore: {
                    commonjs: 'underscore',
                    commonjs2: 'underscore',
                    amd: 'underscore',
                    root: '_'
                }
            },
            {
                backbone: {
                    commonjs: 'backbone',
                    commonjs2: 'backbone',
                    amd: 'backbone',
                    root: 'Backbone'
                }
            },
            "_",
            {
                jquery: {
                    commonjs: 'jquery',
                    commonjs2: 'jquery',
                    amd: 'jquery',
                    root: 'jQuery'
                }
            },
            'jQuery',
            'morningstar-design-system',
            'portfolio-components-common'
        ]

    }
};